<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 商品信息
 * 
 * @author  : $Author: spike $
 * @date    : $Date: 2014-08-06 17:05:36 +0800 (三, 2014-08-06) $
 * @version : $Id: goods.php 126 2014-08-06 09:05:36Z spike $
 * @rev     : $Revision: 126 $
 */
class Goods extends CI_Controller 
{  
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * [浏览]
     */
    public function browse()
    {
        $params = $this->input->get();
        if($params)
        {
            $this->load->model('Goods_model');
            $list = $this->Goods_model->browse_goods($params, $params['page'], $params['size']);

            $this->load->model('Brand_model');
            $this->Brand_model->wrap_brand_info($list['data']);

            $this->load->model('Category_model');
            $this->Category_model->wrap_category_info($list['data']);

            $this->load->library('pagination');
            $config['total_rows'] = $list['total'];
            $config['per_page']   = $list['size'];
            $config['suffix']     = '#data-table';

            $this->pagination->initialize($config); 
            $pagination = $this->pagination->create_links();
        }

        // 统计
        $this->load->dao('Goods_dao');
        $total = $this->Goods_dao->count(array(
            'is_delete' => UNDELETED
        ));

        // 商品筛选数据
        $goods_status = $this->config->item('goods_status');
        $goods_type   = $this->config->item('goods_type');

        $this->load->dao('Brand_dao');
        $brand_sn = $this->Brand_dao->get(
            array('is_delete'=>UNDELETED,'status'=>BRAND_STATUS_ON),
            array('brand_sn','name')
        );

        // 属性模版
        $this->load->dao('Attr_group_dao');
        $attr_group = $this->Attr_group_dao->get(
            array('is_delete'=>UNDELETED,'status'=>ATTR_GROUP_STATUS_ON),
            array('attr_group_id','name')
        );

        // 属性
        if( ! empty($params['attr_group_id']) )
        {
            $this->load->model('Attr_model');
            $ag   = $this->Attr_model->get_attr_group($params['attr_group_id'], TRUE, TRUE);
            $attr = $ag['children'];
        }
        // 属性值
        if( ! empty($params['attr_id']) )
        {
            $this->load->model('Attr_model');
            $av         = $this->Attr_model->get_attr($params['attr_id'], TRUE, TRUE);
            $attr_value = $av['children'];
        }

        $this->load->model('Category_model');
        $category = $this->Category_model->get_category_by_depth(CATEGORY_MAX_DEPTH, FALSE);

        $data = array(
            'default'      => $params ? $params : array(),
            'total'        => $total,
            'list'         => $list,
            'pagesize'     => $this->config->item('pagesize'),
            'goods_status' => $goods_status,
            'goods_type'   => $goods_type,
            'brand_sn'     => $brand_sn,
            'attr'         => $attr,
            'attr_value'   => $attr_value,
            'attr_group'   => $attr_group,
            'category'     => $category,
            'pagination'   => $pagination,
        );

        $this->load->view('home/header.tpl', $data);
        $this->load->view('goods/browse.tpl');
        $this->load->view('home/footer.tpl');
    }

    /**
     * [新增]
     */
    public function add()
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp('add_goods', $params);

            $this->load->model('Goods_model');

            if( FALSE === $goods_sn = $this->Goods_model->add_goods($params) )
            {
                show_error($this->Goods_model->get_error(), 200, '错误', 'error_general_lite');
            }

            temp('add_goods', NULL);

            show_message(
                "新增分类成功！", 
                array(
                    '继续编辑商品'   => DOMAIN."goods/add",
                    '查看新建的商品' => DOMAIN."goods/edit/{$goods_sn}",
                ),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            // 获取分类
            $this->load->model('Category_model');
            $category = $this->Category_model->get_category_by_depth(CATEGORY_MAX_DEPTH, FALSE);

            // 获取临时模版
            $temp = temp('add_goods');

            // 获取分类的属性列表
            if( $temp['category_id'] )
            {
                $this->load->model('Attr_model');
                $attr = $this->Attr_model->get_attr_by_category($temp['category_id'], TRUE, TRUE);
            }
            // 获取品牌
            $this->load->dao('Brand_dao');
            $brand = $this->Brand_dao->get(
                array(
                    'status'    => BRAND_STATUS_ON,
                    'is_delete' => UNDELETED,
                ),
                array(
                    'brand_sn', 'name', 'name_en'
                )
            );

            $page_style    = $this->config->item('page_style');
            $data = array(
                'temp'       => $temp,
                'brand'      => $brand,
                'page_style' => $page_style['LITE'],
                'goods_type' => $this->config->item('goods_type'), 
                'category'   => $category,
                'attr'       => $attr,
                'js'         => array(
                    'jquery/jquery-ueditor-mini/umeditor.config.js',
                    'jquery/jquery-ueditor-mini/umeditor.js',
                ),
                'css'        => array(JS_DOMAIN.'jquery/jquery-ueditor-mini/themes/default/css/umeditor.css'),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('goods/add.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [临时保存]
     */
    public function add_temp()
    {
        $params = $this->input->post();
        temp('add_goods', $params);
        json_exit('操作成功', true);
    }

    /**
     * [临时保存]
     */
    public function edit_temp()
    {
        $params = $this->input->post();
        $key    = "edit_goods_{$params['goods_sn']}";
        temp($key, $params);
        json_exit('操作成功', true);
    }

    /**
     * [编辑]
     */
    public function edit($sn = NULL)
    {
        if($this->input->is_post_request())
        {
            $params = $this->input->post();

            temp("edit_goods_{$params['goods_sn']}", NULL);

            $this->load->model('Goods_model');
            if( FALSE === $this->Goods_model->edit_goods($sn, $params) )
            {
                show_error($this->Goods_model->get_error(), 200, '错误', 'error_general_lite');
            }

            show_message(
                "编辑商品成功！", 
                array('查看编辑的商品' => DOMAIN."goods/edit/{$sn}",),
                200,
                '系统提示',
                'message_general_lite'
            );
        }
        else
        {
            $this->load->model('Goods_model');
            if( ! $goods = $this->Goods_model->get_goods($sn, FALSE, TRUE, TRUE))
            {
                show_error($this->Goods_model->get_error(), 200, '错误', 'error_general_lite');
            }

            // 获取分类
            $this->load->model('Category_model');
            $category = $this->Category_model->get_category_by_depth(CATEGORY_MAX_DEPTH, FALSE);

            $temp = temp("edit_goods_{$goods['goods_sn']}");

            if( validate($temp['category_id'], 'category_id'))
            {
                // 属性列表
                $this->load->model('Attr_model');
                $attr = $this->Attr_model->get_attr_by_category($temp['category_id'], TRUE, TRUE);

                $attr_ids = array_pick($attr, 'attr_id');

                // 所有属性值
                $this->load->dao('Goods_attr_dao');
                $attr_value = $this->Goods_attr_dao->get_in(
                    array('attr_id' => $attr_ids),
                    array('goods_sn' => $goods['goods_sn'])
                );

                $attr_value  = array_pick($attr_value, 'attr_value_id');
                $category_id = $temp['category_id'];
            }
            else
            {
                // 属性列表
                $this->load->model('Attr_model');
                $attr = $this->Attr_model->get_attr_by_category($goods['category_id'], TRUE, TRUE);

                // 所有属性值
                $attr_value = array();
                if($goods['attr'])
                {
                    foreach ($goods['attr'] as $a) 
                    {
                        foreach ($a as $av) 
                        {
                            $attr_value[] = $av;
                        }
                    }
                }

                $category_id = $goods['category_id'];
            }

            // 获取品牌
            $this->load->dao('Brand_dao');
            $brand = $this->Brand_dao->get(
                array(
                    'status'    => BRAND_STATUS_ON,
                    'is_delete' => UNDELETED,
                ),
                array(
                    'brand_sn', 'name', 'name_en'
                )
            );
            $this->load->model('Goods_model');
            $page_style    = $this->config->item('page_style');
            $data = array(
                'category'   => $category,
                'category_id'=> $category_id,
                'attr'       => $attr,
                'attr_value' => $attr_value,
                'goods_type' => $this->config->item('goods_type'),
                'brand'      => $brand,
                'goods'      => $goods,
                'max_img'    => Goods_model::$MAX_IMG,
                'page_style' => $page_style['LITE'],
                'js'         => array(
                    'jquery/jquery-ueditor-mini/umeditor.config.js',
                    'jquery/jquery-ueditor-mini/umeditor.js',
                ),
                'css'        => array(JS_DOMAIN.'jquery/jquery-ueditor-mini/themes/default/css/umeditor.css'),
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('goods/edit.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [批量改变状态]
     */
    public function batch_change_status($id = NULL)
    {
        if( $this->input->is_post_request() )
        {
            $goods_sns = $this->input->post('goods_sn');
            $act       = $this->input->post('act');

            $this->load->model('Goods_model');
            if( FALSE === $affected_row = $this->Goods_model->batch_change_goods_status($goods_sns, $act) )
            {
                show_error($this->Goods_model->get_error(), 200, '错误', 'error_general_lite');
            }
            else
            {
                show_message(
                    "成功编辑了[{$affected_row}]个商品的状态！", 
                    array(),
                    200,
                    '系统提示',
                    'message_general_lite'
                );
            }
        }
        else
        {
            $goods_sn   = $this->input->get('goods_sn');
            $page_style = $this->config->item('page_style');

            $data = array(
                'goods_sn'     => $goods_sn,
                'page_style'   => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('goods/batch_change_status.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [批量改变状态]
     */
    public function batch_change_brand($id = NULL)
    {
        if( $this->input->is_post_request() )
        {
            $goods_sns = $this->input->post('goods_sn');
            $brand_sn  = $this->input->post('brand_sn');

            $this->load->model('Goods_model');
            if( FALSE === $affected_row = $this->Goods_model->batch_change_goods_brand($goods_sns, $brand_sn) )
            {
                show_error($this->Goods_model->get_error(), 200, '错误', 'error_general_lite');
            }
            else
            {
                show_message(
                    "成功编辑了[{$affected_row}]个商品的品牌！", 
                    array(),
                    200,
                    '系统提示',
                    'message_general_lite'
                );
            }
        }
        else
        {
            $goods_sn   = $this->input->get('goods_sn');
            $page_style = $this->config->item('page_style');

            $this->load->dao('Brand_dao');
            $brand = $this->Brand_dao->get(
                array('is_delete'=>UNDELETED, 'status'=>BRAND_STATUS_ON)
            );
            $data = array(
                'brand'      => $brand,
                'goods_sn'   => $goods_sn,
                'page_style' => $page_style['LITE'],
            );
            $this->load->view('home/header.tpl', $data);
            $this->load->view('goods/batch_change_brand.tpl');
            $this->load->view('home/footer.tpl');
        }
    }

    /**
     * [改变状态]
     */
    public function change_status($sn = NULL)
    {
        $act     = $this->input->get('act');
        $is_ajax = $this->input->get('is_ajax');

        $this->load->model('Goods_model');
        if( FALSE === $this->Goods_model->change_goods_status($sn, $act))
        {
            if($is_ajax === '1')
            {
                json_exit($this->Goods_model->get_error());
            }
        }
        else
        {
            if($is_ajax === '1')
            {
                json_exit('操作成功', TRUE);
            }
        }
    }

    /**
     * [获取属性, 属性值]
     */
    public function get_attr()
    {
        $act       = $this->input->get('act');
        $parent_id = $this->input->get('parent_id');
        $is_ajax   = $this->input->get('is_ajax');

        switch ( strtolower( trim($act) ) )
        {
            case 'attr':
                    $this->load->model('Attr_model');
                    if( FALSE === $attr_group = $this->Attr_model->get_attr_group($parent_id, TRUE, TRUE) )
                    {
                        if($is_ajax === '1')
                        {
                            json_exit($this->Attr_model->get_error());
                        }
                    }
                    $attr = array();
                    if($attr_group)
                    {
                        foreach ($attr_group['children'] as $v) 
                        {
                            $attr[] = array(
                                'id'   => $v['attr_id'],
                                'name' => $v['name'],
                            );
                        }
                    }

                    json_exit('操作成功', TRUE, $attr);
                break;
            case 'attr_value':
                    $this->load->model('Attr_model');
                    if( FALSE === $attr = $this->Attr_model->get_attr($parent_id, TRUE, TRUE) )
                    {
                        if($is_ajax === '1')
                        {
                            json_exit($this->Attr_model->get_error());
                        }
                    }

                    $attr_value = array();
                    if($attr)
                    {
                        foreach ($attr['children'] as $v) 
                        {
                            $attr_value[] = array(
                                'id'   => $v['attr_value_id'],
                                'name' => $v['name'],
                            );
                        }
                    }

                    json_exit('操作成功', TRUE, $attr_value);
                break;

            default:
                    if($is_ajax === '1')
                    {
                        json_exit("获取属性值失败：未知的操作类型");
                    }
                break;
        }
    }

}
/* End of file */